import Cookies from 'js-cookie'

export default {
    data() {
        return {
            dialogUploadVisible: false,
            uploadurl: this.$api.Resource.resourceUploadUrl,
            fileList: [],
            loading: false,

            resourceList: [],

            // 分页参数
            currentPage: 1,
            pageSize: 10,
            total: 0,

        }
    },

    methods: {

        dataFormat(row, column) {
            const value = row.createTime
            var year = value.substr(0, 4)
            var month = value.substr(5, 2)
            var day = value.substr(8, 2)
                // var hour = value.substr(8, 2)
                // var minute = value.substr(10, 2)
                // var second = value.substr(12, 2)
            return year + '-' + month + '-' + day;
        },

        nameFormatter(row, column) {
            const value = row.author;
            if (value == "bcc") {
                return "鲍辰聪";
            } else {
                return "李根生";
            }
        },

        submitUpload() {
            if (this.fileList.length != 0) {
                this.$refs.upload.submit();
            } else {
                this.$message.error('请先选择文件!');
            }
        },
        // 上传前触发的函数
        beforeUpload(file) {
            const isMP4 = file.type === 'video/mp4';
            if (!isMP4) {
                this.$message.error('上传头像图片只能是 MP4 格式!');
            }
            return isMP4;
        },
        // 选择文件改变时触发的函数
        handleChange(file, fileList) {
            this.fileList = [fileList[fileList.length - 1]];
        },
        // 点击上传服务器触发事件
        uploadSectionFile(param) {
            var fileObj = param.file;
            // FormData 对象
            var form = new FormData();
            // 文件对象
            form.append("file", fileObj);

            if (!Cookies.get("ticket")) {
                this.$message.warning('请先登录再上传!');
                return false;
            }

            this.loading = true;

            //form.append("filename", this.fileList[0].name);
            this.$api.Resource.resourceUpload(form, Cookies.get("ticket")).then(res => {
                this.loading = false;
                if (res.code == 200) {
                    this.dialogUploadVisible = false;
                    this.fileList = [];
                    this.$message({
                        message: '上传成功',
                        type: 'success'
                    });
                    this.getResourceTotal();
                    this.getResourceList();
                } else {
                    this.$message.error("上传失败")
                }
            }).catch(erroe => {
                this.loading = false;
            });
        },
        // 点击下载触发事件
        handleDownload(index, row) {

            if (!Cookies.get("ticket")) {
                this.$message.warning("请先登录再下载")
                return false;
            }

            this.$notify.info({
                title: '获取中',
                message: '获取数据流中...'
            });
            this.$api.Resource.resourceDownload(row.id, Cookies.get("ticket")).then(res => {
                this.saveFile(res, row.name);
                this.$notify({
                    title: '成功',
                    message: '下载成功！',
                    type: 'success'
                });
            }).catch(error => {
                this.$message.error('下载失败!');
            });

        },
        saveFile(data, name) {
            try {
                const blobUrl = window.URL.createObjectURL(data)
                const a = document.createElement('a')
                a.style.display = 'none'
                a.download = name
                a.href = blobUrl
                a.click()
            } catch (e) {
                this.$message.error('保存文件出错')
            }
        },

        // 点击文件名触发事件
        handlePlay(index, row) {
            this.$router.push({
                path: '/video/player',
                query: {
                    id: row.id,
                    //filename: row.name
                }
            });
        },
        // 点击删除触发事件
        handleDelete(index, row) {

            if (!Cookies.get("ticket")) {
                this.$message.warning("请先登录再删除")
                return false;
            }

            this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.delResource(row.id);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 每页资源数量改变触发事件
        handleSizeChange(val) {
            this.pageSize = val;
            this.getResourceList();
        },
        // 当前页改变触发事件
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getResourceList();
        },
        // 获取资源总数
        getResourceTotal() {
            this.$api.Resource.resourceTotal(Cookies.get("ticket")).then(res => {
                if (res.code == 200) {
                    this.total = res.data.count;
                } else if (res.code == 400) {
                    this.$message.error("获取资源总数失败")
                }
            })
        },
        // 获取资源列表
        getResourceList() {
            let query = {
                "currentPage": this.currentPage,
                "pageSize": this.pageSize
            };

            this.$api.Resource.queryList(query, Cookies.get("ticket")).then(res => {
                if (res.code == 200) {
                    this.resourceList = res.data.resourceList;
                } else if (res.code == 400) {
                    this.$message.error("获取分页数据失败")
                }
            })
        },
        // 删除资源
        delResource(id) {
            let data = {
                id: id,
            }
            this.$api.Resource.resourceDel(data, Cookies.get("ticket")).then(res => {
                if (res.code == 200) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    this.getResourceTotal();
                    this.getResourceList();
                } else {
                    this.$message.error("删除失败");
                }
            })
        },
    },

    watch: {
        'dialogUploadVisible': function(newVal, oldVal) {
            if (!newVal) {
                this.fileList = [];
            }
        }
    },

    created() {
        this.getResourceTotal();
        this.getResourceList();
    }

}